//
//  FKShareVC.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/1/29.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKShareVC.h"
#import <UShareUI/UShareUI.h>

@interface FKShareVC ()

@property (weak, nonatomic) IBOutlet UIView *header;
@property (weak, nonatomic) IBOutlet UIImageView *bannerView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bannerH;
@property (weak, nonatomic) IBOutlet UILabel *ruleLabel;
@property (nonatomic, strong) NSDictionary *info;

@end

@implementation FKShareVC

- (instancetype)init
{
    return [UIStoryboard storyboardWithName:@"FKShareVC" bundle:nil].instantiateInitialViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.bannerH.constant = fk_adjustW(240);
    [self loadData];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
}

- (IBAction)recommendBtnClicked:(UIButton *)sender
{
    if (!self.info) return;
    
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:self.info[@"share_title"] descr:self.info[@"share_intro"] thumImage:[UIImage imageNamed:@"AppIcon"]];
        shareObject.webpageUrl = self.info[@"share_link"];
        messageObject.shareObject = shareObject;
        [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
            if (error) {
                [SVProgressHUD showErrorWithStatus:@"分享失败"];
            } else {
                [SVProgressHUD showSuccessWithStatus:@"分享成功"];
            }
        }];
    }];
}

#pragma mark - 网络请求
- (void)loadData
{
    [FKHttpTool postWithSuffix:@"m=Home&c=Member&a=spread" param:nil success:^(FKBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        if (result.isSuccess) {
            NSDictionary *ad = result.data[@"ad"];
            NSDictionary *share_rule = result.data[@"share_rule"];
            self.info = result.data[@"info"];
            
            [self.bannerView sd_setImageWithURL:[NSURL URLWithString:ad[@"img"]] placeholderImage:[UIImage imageNamed:@"banner_tuiguang"]];
            self.ruleLabel.text = [share_rule[@"info"] fk_htmlToString];
            CGFloat bannerH = fk_adjustW(240);
            CGFloat ruleTitleH = [@"活动规则" sizeWithAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]}].height;
            CGFloat ruleH = [self.ruleLabel.text boundingRectWithSize:CGSizeMake(fkScreenW - 24, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
            CGFloat headerH = bannerH + 30 + 44 + 30 + ruleTitleH + 20 + ruleH + 30;
            self.header.frame = CGRectMake(0, 0, fkScreenW, headerH);
            self.tableView.tableHeaderView = self.header;
            
        }
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

@end
